package com.googlecode.chigs.repository.jpa.impl;

import org.springframework.security.GrantedAuthority;
import org.springframework.security.GrantedAuthorityImpl;
import org.springframework.security.userdetails.UserDetails;

import com.googlecode.chigs.domain.User;

public class UserServiceImpl implements UserDetails {

	private User user;
	
	public UserServiceImpl(User user) {
		this.user = user;
	}

	@Override
	public GrantedAuthority[] getAuthorities() {
		if (user.isAdmin()) {
			return new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_USER"), new GrantedAuthorityImpl("ROLE_ADMIN")};
		}
		return new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_USER")};
	}

	@Override
	public String getPassword() {
		return user.getPassword();
	}

	@Override
	public String getUsername() {
		return user.getEmail();
	}

	@Override
	public boolean isAccountNonExpired() {
		return true;
	}

	@Override
	public boolean isAccountNonLocked() {
		return true;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		return true;
	}

	@Override
	public boolean isEnabled() {
		return user.isEnabled();
	}

	public User getUser() {
		return user;
	}
	
	public Long getSalt() {
		return user.getId();
	}
}
